
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.dao.ILocationDAO;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.pojo.Orgheads;


public class LocationBOImpl implements ILocationBO {

	public LocationBOImpl() {
		dao = null;
	}

	public Location loadLocation(String locationNo) {
		Object ob = dao.loadObject(com.wyzz.hr.pojo.Location.class, locationNo,
				null, new boolean[0]);
		if (ob == null)
			return null;
		else
			return (Location) ob;
	}

	public List FindAllLocation() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Location.class);
		dc.addOrder(Order.asc("locationSortId"));
		return dao.findByCriteria(dc);
	}

	public List FindEnabledLocation() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Location.class);
		dc.add(Restrictions.eq(Location.PROP_LOCATION_STATUS, new Integer(1)));
		dc.addOrder(Order.asc("locationSortId"));
		return dao.findByCriteria(dc);
	}

	public boolean addLocation(Location location) {
		String trimmedLocationName;
		trimmedLocationName = location.getLocationName().trim();
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Location.class);
		detachedCriteria.add(Restrictions.eq("locationName",
				trimmedLocationName));
		List old = dao.findByCriteria(detachedCriteria);
		if (!old.isEmpty())
			return false;
		try {
			location.setLocationName(trimmedLocationName);
			dao.saveObject(location);
			if (StringUtils.isNotEmpty(location.getHeadNo())) {
				Orgheads head = new Orgheads();
				head.setOrgheadsEmpNo(location.getHeadNo());
				head.setOrgheadsOrgNo(location.getId());
				head.setOrgheadsResponsibility("locationheader");
				dao.saveObject(head);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String delLocation(Class name, String id) {
		StringBuffer buffer = new StringBuffer();
		try {
			String hql = (new StringBuilder()).append(
					"select count(*) from Employee where empLocationNo='")
					.append(id).append("'").toString();
			List check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u5458\u5DE5\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			hql = (new StringBuilder())
					.append(
							"select count(*) from BenefitType where benefitTypeLocNo='")
					.append(id).append("'").toString();
			check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u793E\u4FDD\u79CD\u7C7B\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			hql = (new StringBuilder()).append(
					"select count(*) from Empsalarypay where espDept='")
					.append(id).append("'").toString();
			check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u85AA\u8D44\u6570\u636E\u53D1\u653E\u8BB0\u5F55\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			hql = (new StringBuilder()).append(
					"select count(*) from Leavecalendar where lcLocationNo='")
					.append(id).append("'").toString();
			check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u65E5\u5386\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			hql = (new StringBuilder())
					.append(
							"select count(*) from Leaverequest where lrEmpLocationNo='")
					.append(id).append("'").toString();
			check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u8BF7\u5047\u8BB0\u5F55\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			hql = (new StringBuilder())
					.append(
							"select count(*) from Overtimerequest where orEmpLocationNo='")
					.append(id).append("'").toString();
			check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u52A0\u73ED\u8BB0\u5F55\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			hql = (new StringBuilder())
					.append(
							"select count(*) from Recruitplan where recpWorkLocation='")
					.append(id).append("'").toString();
			check = dao.exeHqlList(hql);
			if (hasReleasation(check))
				buffer
						.append("\u6709\u62DB\u8058\u8BA1\u5212\u5173\u8054\u5230\u8BE5\u5730\u533A\uFF01");
			if (StringUtils.isEmpty(buffer.toString()))
				dao.deleteObject(com.wyzz.hr.pojo.Location.class, id);
			hql = (new StringBuilder()).append(
					"delete from Orgheads where orgheadsOrgNo='").append(id)
					.append("'").toString();
			dao.exeHql(hql);
			return buffer.toString();
		} catch (Exception e) {
			buffer.append((new StringBuilder()).append(
					"\u7CFB\u7EDF\u5F02\u5E38").append(e.getMessage())
					.toString());
		}
		return buffer.toString();
	}

	private boolean hasReleasation(List list) {
		return Integer.valueOf(list.get(0).toString()).intValue() > 0;
	}

	public String updateLocation(Location location) {
		if (location.getLocationStatus().intValue() == 0) {
			String hql = (new StringBuilder()).append(
					"select count(*) from Employee where empLocationNo='")
					.append(location.getId()).append("'").toString();
			List check = dao.exeHqlList(hql);
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				return "1";
		}
		String trimmedLocationName = location.getLocationName().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Location.class);
		dc.add(Restrictions
				.eq(Location.PROP_LOCATION_NAME, trimmedLocationName));
		dc.add(Restrictions.ne(Location.PROP_ID, location.getId()));
		List old = dao.findByCriteria(dc);
		if (!old.isEmpty())
			return "2";
		Location oldLocation = (Location) dao.loadObject(
				com.wyzz.hr.pojo.Location.class, location.getId(), null,
				new boolean[0]);
		oldLocation.setLocationDesc(location.getLocationDesc());
		oldLocation.setLocationName(trimmedLocationName);
		oldLocation.setLocationStatus(location.getLocationStatus());
		oldLocation.setLocationSortId(location.getLocationSortId());
		dao.updateObject(oldLocation);
		if (StringUtils.isNotEmpty(location.getHeadNo())) {
			DetachedCriteria detachedCriteria = DetachedCriteria
					.forClass(com.wyzz.hr.pojo.Orgheads.class);
			detachedCriteria.add(Restrictions.eq("orgheadsOrgNo", oldLocation
					.getId()));
			detachedCriteria.add(Restrictions.eq("orgheadsResponsibility",
					"locationheader"));
			List oldLocationHead = dao.findByCriteria(detachedCriteria);
			if (oldLocationHead.isEmpty()) {
				Orgheads head = new Orgheads();
				head.setOrgheadsEmpNo(location.getHeadNo());
				head.setOrgheadsOrgNo(oldLocation.getId());
				head.setOrgheadsResponsibility("locationheader");
				dao.saveObject(head);
			} else {
				Orgheads head = (Orgheads) oldLocationHead.get(0);
				head.setOrgheadsEmpNo(location.getHeadNo());
				dao.updateObject(head);
			}
		}
		return "0";
	}

	public void updateLocationSwapSortedId(String tr1Id, String tr2Id) {
		Location swapLocationOne = (Location) dao.loadObject(
				com.wyzz.hr.pojo.Location.class, tr1Id, null, new boolean[0]);
		Location swapLocationTwo = (Location) dao.loadObject(
				com.wyzz.hr.pojo.Location.class, tr2Id, null, new boolean[0]);
		int tempSortId = swapLocationOne.getLocationSortId().intValue();
		swapLocationOne.setLocationSortId(swapLocationTwo.getLocationSortId());
		swapLocationTwo.setLocationSortId(Integer.valueOf(tempSortId));
		dao.updateObject(swapLocationOne);
		dao.updateObject(swapLocationTwo);
	}

	public void saveLocationSortIdByBatch(String ids[]) {
		int length = ids.length;
		int sortId = 1;
		for (int i = 0; i < length; i++) {
			dao.exeHql((new StringBuilder()).append(
					"update Location set locationSortId=").append(sortId)
					.append(" where id ='").append(ids[i]).append("'")
					.toString());
			sortId++;
		}

	}

	public ILocationDAO getDao() {
		return dao;
	}

	public void setDao(ILocationDAO dao) {
		this.dao = dao;
	}

	private ILocationDAO dao;
}
